/**
 * Created by dywu on 2015/9/24.
 */
angular.module('preferential.userbalance', [
])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('preferential.list', {
            url: '/balance/list',
            templateUrl: usercenter_viewurl+"/balance/list.html",
            resolve: {
                activity_list : ['$stateParams','UserBalanceService',function($stateParams, UserBalanceService){
                    var page={
                        pageIndex:'',
                        pageSize:'',
                    }
                    return UserBalanceService.getactivityall(page).$promise;
                }]
            },
            controller: 'UserBalanceController'
        })
        .state('preferential.add', {
            url: '/balance/add',
            templateUrl: usercenter_viewurl+"/balance/add.html",
            controller: 'ActivityAddController'
        })

        .state('preferential.activitydetail', {
            url: '/balance/detail/:activityid',
            templateUrl: usercenter_viewurl+"/balance/detail.html",
            resolve: {
                activity_id : ['$stateParams','UserBalanceService',function($stateParams, UserBalanceService){
                    return UserBalanceService.getactivitybyid($stateParams.activityid).$promise;
                }]
            },
            controller: 'ActivityDetailController'
        })


        .state('preferential.activityadvise', {
            url: '/balance/advise/:activityid',
            templateUrl: usercenter_viewurl+"/balance/advise.html",
            resolve: {
                activity_advise : ['$stateParams','UserBalanceService',function($stateParams, UserBalanceService){
                    return UserBalanceService.getactivitybyid($stateParams.activityid).$promise;
                }]
            },
            controller: 'ActivityAdviseController'
        })

}])


    .controller('ActivityAdviseController', ['$scope','activity_advise','UserBalanceService',function($scope,activity_advise,UserBalanceService) {

        // 满减金额 打折显示默认数据
        if(!activity_advise.activitymoney && !activity_advise.activitygoods && activity_advise.activitydiscount ){
            activity_advise.defaultchoise='活动打折';
            activity_advise.price = activity_advise.activitydiscount;
            $scope.adviseselect = false;
            $scope.adviseprice = false;
            $scope.advisegoods = true;
        }
        else if(activity_advise.activitymoney && !activity_advise.activitygoods && !activity_advise.activitydiscount){
            activity_advise.defaultchoise='活动金额';
            activity_advise.price = activity_advise.activitymoney;
            $scope.adviseselect = false;
            $scope.adviseprice = false;
            $scope.advisegoods = true;
        }
        else if(!activity_advise.activitymoney && activity_advise.activitygoods && activity_advise.activitydiscount) {
            activity_advise.defaultchoise='活动打折及送物品';
            activity_advise.price = activity_advise.activitydiscount;
            activity_advise.goods = activity_advise.activitygoods;
            $scope.adviseselect = false;
            $scope.adviseprice = false;
            $scope.advisegoods = false;
        }
        else if(activity_advise.activitymoney && activity_advise.activitygoods && !activity_advise.activitydiscount) {
            activity_advise.defaultchoise='活动金额及送物品';
            activity_advise.price = activity_advise.activitymoney;
            activity_advise.goods = activity_advise.activitygoods;
            $scope.adviseselect = false;
            $scope.adviseprice = false;
            $scope.advisegoods = false;
        }
        else if(!activity_advise.activitymoney && activity_advise.activitygoods && !activity_advise.activitydiscount) {
            activity_advise.defaultchoise='只送物品';
            activity_advise.goods = activity_advise.activitygoods;
            $scope.adviseselect = false;
            $scope.adviseprice = true;
            $scope.advisegoods = false;
        }else {
            activity_advise.defaultchoise='请选择';
            $scope.adviseselect = true;
            $scope.adviseprice = true;
            $scope.advisegoods = true;
        }

        if(activity_advise.acitivitywhethercondition)activity_advise.advisecondition = '活动可叠加';
        else activity_advise.advisecondition = '活动不可叠加'

        $scope.adviseactivity = activity_advise;
        console.log($scope.adviseactivity )
        //  修改选择
        $scope.advisechose = function (stack) {
            console.log(stack);
            switch(stack){
                case '1':
                    $scope.adviseselect = false;
                    $scope.adviseprice = false;
                    $scope.advisegoods = true;
                    $scope.deviseplaceholder = "打折数字范围在0~9.9折";
                    break;

                case '2':
                    $scope.adviseselect = false;
                    $scope.adviseprice = false;
                    $scope.advisegoods = true;
                    $scope.deviseplaceholder = "不能超过满减金额";
                    break;

                case '3':
                    $scope.deviseplaceholder = "打折数字范围在0~9.9折";
                    $scope.adviseselect = false;
                    $scope.adviseprice = false;
                    $scope.advisegoods = false;

                    break;

                case '4':
                    $scope.adviseselect = false;
                    $scope.adviseprice = false;
                    $scope.advisegoods = false;
                    $scope.deviseplaceholder = "不能超过满减金额";
                    break;

                case '5':
                    $scope.adviseselect = false;
                    $scope.adviseprice = true;
                    $scope.advisegoods = false;

                    break;

                default :
                    $scope.adviseselect = true;
                    $scope.adviseprice = false;
                    $scope.advisegoods = false;
                    break;
            }
        }

        var d = new Date();
        $scope.start_time_show=d.getFullYear() + "-" + (d.getMonth()-1) + "-" + d.getDate();
        $scope.end_time_show=d.getFullYear() + "-" + (d.getMonth()+1) + "-" + (d.getDate()+1);

        var e = new Date($scope.end_time_show.replace(/-/g, '/'));
        var s = new Date($scope.start_time_show.replace(/-/g, '/'));
        $scope.start_time=s.getTime();
        $scope.end_time=e.getTime();
        $scope.end_time_show=d.getFullYear() + "-" + (d.getMonth()+1) + "-" + (d.getDate()+1);
        $('#advisestarttime').datetimepicker({
            format: "yyyy-MM-dd hh:ii:mm",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.end_time = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $('#adviseendtime').datetimepicker({
            format: "yyyy-MM-dd hh:ii:mm",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.end_time = ev.date.valueOf();
            // console.log($scope.end_time); 添加优惠券

            //console.log(ev.date.valueOf());
        });



        $scope.activityadvise = function(advise){
            console.log('advise',advise);

            // 修改活动标题
            var devisetitle = {
                activitytitle:advise.activitytitle,
                activitysubhead:advise.activitysubhead,
                activitycity:advise.activitycity
            }
            UserBalanceService.devisertitle(advise.activityid,devisetitle, function (data) {

                },function(data){
                    alert("修改失败");
                }

            );


            // 修改活动时间
            if(!advise.starttime)advise.starttime = minuteTodatetime(advise.activitystarttime);
            if(!advise.endtime)advise.endtime = minuteTodatetime(advise.activityterminationtime);
            var advisetime={
                startTime:advise.starttime,
                terminationTime:advise.endtime
            }

            UserBalanceService.advisetime(advise.activityid,advisetime,function(data){

            },function(data){
                alert("时间修改失败");
            });


            //活动叠加数据处理
            if( '1' == advise.orfial )advise.devisecondition = false;                           // 修改后活动不可叠加
            else if( '2' == advise.orfial )advise.devisecondition = true;                       // 修改后活动可叠加
            else advise.devisecondition = advise.acitivitywhethercondition;                     // 无修改活动情况

            var deviserule = {
                acitivitywhethercondition:advise.devisecondition,
                activityfrequency:advise.activityfrequency,
                activitywhethermember:false
            }

            UserBalanceService.devisercondition(advise.activityid,deviserule, function (data) {
                    history.go(-1);
                },function(data){
                    alert("修改失败");
                }

            );

             //修改活动规则
            switch(advise.stack){

                // 活动打折
                case '1':
                    advise.discount = advise.price;
                    advise.money = null;
                    advise.goods = null;
                    break;
                // 活动金额
                case '2':
                    advise.discount = null;
                    advise.money = advise.price;
                    advise.goods = null;
                    break;

                case '3':
                    advise.discount = advise.price;
                    advise.money = null;
                    advise.goods = advise.sendgoods;
                    break;
                //
                case '4':
                    advise.discount = null;
                    advise.money = advise.price;
                    advise.goods = advise.activitygoods;
                    break;
                // 只送物品
                case '5':
                    advise.discount = null;
                    advise.money = null;
                    advise.goods = advise.activitygoods;
                    break;
                // 不修改的
                default:
                    advise.discount = advise.activitydiscount;
                    advise.money = advise.activitymoney;
                    advise.goods = advise.activitygoods;
                    break;
            }

            var deviserule = {
                activityfullcut:advise.activityfullcut,
                avtivitydiscount:advise.discount,
                activitymoney:advise.money,
                activitygoods:advise.goods
            }
            UserBalanceService.deviserule(advise.activityid,deviserule,function(data){
                alert("修改成功");
            },function(data){
                alert("修改失败");
            })
        }

        var minuteTodatetime = function(date){
            var data =new Date(date);
            var year =data.getFullYear();
            var month =data.getMonth()+1;
            var day =data.getDate();
            var hour =data.getHours();
            var minute =data.getMinutes();
            var second =data.getSeconds();
            return (year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
        }

    }])

    .controller('ActivityDetailController', ['$scope', 'activity_id',function($scope,activity_id) {

        if( activity_id.activitystack )  activity_id.stack = '活动生效';
        else activity_id.stack = '活动失效';
        if( activity_id.activitygoods )  activity_id.sendgoods = activity_id.activitygoods;
        else activity_id.sendgoods = '无';
        if( activity_id.acitivitywhethercondition) activity_id.condition = '活动可叠加';
        else activity_id.condition = '活动不可叠加';
        console.log(activity_id)
        $scope.activitydetail = activity_id;

    }])


    .controller('UserBalanceController', ['$scope', 'activity_list','UserBalanceService',function($scope,activity_list,UserBalanceService) {
        for( var i = 0 ;i < activity_list.length ; i++ ){
            if( !activity_list[i].activitymoney && activity_list[i].activitydiscount){
                activity_list[i].rule =   '打'+activity_list[i].activitydiscount+'折';
            }else if(  activity_list[i].activitymoney  && !activity_list[i].activitydiscount ){
                activity_list[i].rule =   '减'+ activity_list[i].activitymoney + '元';
            }else if(activity_list[i].activitygoods){
                activity_list[i].rule = '送'+activity_list[i].activitygoods;
            }
        }
        $scope.getactivitylist = activity_list;

        console.log($scope.getactivitylist)

        $scope.activityremove = function(activityid){
            console.log($scope.getactivitylist.length)
            var indextodelete = -1;
            for (var i=0;i<$scope.getactivitylist.length;i++) {
                if ($scope.getactivitylist[i].activityid == activityid) {
                    //从数组中删除这个id的�??
                    indextodelete = i;
                    break;
                }
            }
            UserBalanceService.deleteactivity(activityid).$promise.then(function(date){
                if (indextodelete >= 0) {
                    $scope.getactivitylist.splice(indextodelete, 1);
                }
                alert("删除活动成功", 1000);
            },function(date){
                alert("删除失败");
            });
        }
}])




.controller('ActivityAddController', ['$scope', 'UserBalanceService',function($scope,UserBalanceService) {

    console.log("ActivityAddController");

             var d = new Date();
        $scope.start_time_show=d.getFullYear() + "-" + (d.getMonth()-1) + "-" + d.getDate();
        $scope.end_time_show=d.getFullYear() + "-" + (d.getMonth()+1) + "-" + (d.getDate()+1);

        var e = new Date($scope.end_time_show.replace(/-/g, '/'));
        var s = new Date($scope.start_time_show.replace(/-/g, '/'));
        $scope.start_time=s.getTime();
        $scope.end_time=e.getTime();
        $scope.end_time_show=d.getFullYear() + "-" + (d.getMonth()+1) + "-" + (d.getDate()+1);
        $('#addstarttime').datetimepicker({
            format: "yyyy-MM-dd hh:ii:mm",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.end_time = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $('#addendtime').datetimepicker({
            format: "yyyy-MM-dd hh:ii:mm",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.end_time = ev.date.valueOf();
            // console.log($scope.end_time);
            //console.log(ev.date.valueOf());
        });


        // 选择是 满减 / 打折 / 无
        $scope.selectfullstact = function(stack){
            switch(stack){
                case '1':
                    $scope.activityselect = false;
                    $scope.activityvisible = false;
                    $scope.activityplaceholder="例:85代表8.5折";
                    break;
                case '2':
                    $scope.activityselect = false;
                    $scope.activityvisible = false;
                    $scope.activityplaceholder="金额不能大于活动满减";
                    break;
                case '3':
                    $scope.activityselect = false;
                    $scope.activityvisible = true;
                    $scope.activitycomplate.price = null;
                    break;
                default:
                    $scope.activityselect = true;
                    $scope.activityvisible = false;
                    break;
            }

        }



        // 創建活動條件
        $scope.add=function(activitycomplate){


            console.log('activitycomplate',activitycomplate);
            var rule ={
                discount:null,
                price:null
            };
            switch(activitycomplate.fullstact){
                case '1':
                    rule.discount = activitycomplate.price / 10;
                    rule.price = null;
                    break;
                case '2':
                    rule.discount = null;
                    rule.price = activitycomplate.price;
                    break;
                case '3':
                    rule.discount = null;
                    rule.price = null;
                    break;
            }
            var activitycode ={
                activitytitle:activitycomplate.title,
                activitysubhead:activitycomplate.subhead,
                activitycity:activitycomplate.citycode,
                activitystarttime:activitycomplate.starttime,
                activityterminationtime:activitycomplate.ternimation,
                activityfullcut:activitycomplate.fullcut,
                activityfrequency:activitycomplate.frequency,
                activitywhethermember:false,
                acitivitywhethercondition:activitycomplate.condition,
                avtivitydiscount:rule.discount,
                activitymoney:rule.price,
                activitygoods:activitycomplate.goods
            };
            console.log('activitycode',activitycode);
            UserBalanceService.activityAdd(activitycode),function(data){
                alert("添加成功");
                console.log('data',data);
            },function(data){
                alert("添加失败！"+data.msg);
            };

        }



}])
;
